import { Body, Controller, Post, Get, Headers, Query } from '@nestjs/common';
import { getResponse, ERROR_CODE, ERROR_MESSAGE } from 'src/common/const/const';
import { AuthService } from 'src/services/Auth';
import { PayOrderService } from './payOrder.service';
import { CreatePayOrderDto, FindOnePayOrderDto } from './dto/payOrder.dto';
import { GoodsNotFoundError, PayOrderFoundError, GoodsRepeatablePurchaseError } from 'src/common/utils/error';
import { serviceInfoLog,serviceErrorLog } from "src/common/out/logger/access.logger";

@Controller('payOrder')
export class PayOrderController {
    constructor(
        private readonly payOrderService: PayOrderService,
        private readonly authService: AuthService,
    ) { }

    // 创建支付订单
    @Post('/create')
    async createPayOrder(@Body() createPayOrderDto: CreatePayOrderDto, @Headers() headers: any) {
        const userInfo = this.authService.checkAuth(headers);
        if (!userInfo) {
            return getResponse(ERROR_CODE.VERIFY_EXPIRE, ERROR_MESSAGE[ERROR_CODE.VERIFY_EXPIRE], {})
        }
        const userid = userInfo.userid
        try {
            let response = await this.payOrderService.createPayOrder(
                userid,
                createPayOrderDto.goodsId,
                createPayOrderDto.payType,
                createPayOrderDto.payChannelUser,
            );
            return getResponse(0, "success", response);
        } catch (error) {
            serviceErrorLog(`createPayOrder error: ${error}`)
            if (error instanceof GoodsNotFoundError) {
                return getResponse(ERROR_CODE.GOODS_NOT_FOUND, ERROR_MESSAGE[ERROR_CODE.GOODS_NOT_FOUND], {})
            } else if (error instanceof GoodsRepeatablePurchaseError) {
                return getResponse(ERROR_CODE.GOODS_NOT_REPEATABLE_PURCHASE, ERROR_MESSAGE[ERROR_CODE.GOODS_NOT_REPEATABLE_PURCHASE], {})
            } else {
                return getResponse(ERROR_CODE.INTERNAL_FAIL, ERROR_MESSAGE[ERROR_CODE.INTERNAL_FAIL], {})
            }
        }
    }

    // 查询订单详情
    @Get('/findOne')
    async findOneOrder(@Query() findOnePayOrderDto: FindOnePayOrderDto, @Headers() headers: any) {
        const userInfo = this.authService.checkAuth(headers);
        if (!userInfo) {
            return getResponse(ERROR_CODE.VERIFY_EXPIRE, ERROR_MESSAGE[ERROR_CODE.VERIFY_EXPIRE], {})
        }

        try {
            const payOrder = await this.payOrderService.findByTransactionId(findOnePayOrderDto.transactionId);
            let payOrderInfo = {
                transactionId: payOrder.transactionId,
                status: payOrder.status
            };
            return getResponse(0, "success", { payOrderInfo });

        } catch (error) {
            serviceErrorLog(`findOneOrder error: ${error}`)
            if (error instanceof PayOrderFoundError) {
                return getResponse(ERROR_CODE.PAY_ORDER_NOT_FOUND, ERROR_MESSAGE[ERROR_CODE.PAY_ORDER_NOT_FOUND], {})
            } else {
                return getResponse(ERROR_CODE.INTERNAL_FAIL, ERROR_MESSAGE[ERROR_CODE.INTERNAL_FAIL], {})
            }
        }

    }

}
